//
//  ViewController.m
//  Share
//
//  Created by 胡文阳 on 17/3/15.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) UIImageView *imageV;
@property (nonatomic,strong) UIView *centerShow;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.imageV];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 70, 30);
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn addTarget:self action:@selector(beginAnimation) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:@"开始动画" forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
}

-(void)beginAnimation
{
    if (_flag == 0)
    {
        [self animation0];
    }
    else if (_flag == 1)
    {
        [self animation1];
    }
    else if (_flag == 2)
    {
        [self animation2];
    }
    else if (_flag == 3)
    {
        [self animation3];
    }
    else if (_flag == 4)
    {
        [self animation4];
    }
    else if (_flag == 5)
    {
        _imageV.hidden = YES;
        self.centerShow = [[UIView alloc]init];
        self.centerShow.center = self.view.center;
        self.centerShow.backgroundColor = [UIColor greenColor];
        self.centerShow.bounds = CGRectMake(0, 0, 100, 100);
        [self.view addSubview:self.centerShow];
        [self animation5];
    }
    else if (_flag == 6)
    {
        [self animation6];
    }
}

//0. 基础写法
-(void)animation0{
    
    //基础动画写法
    
    [UIView beginAnimations:@"frameAnimation" context:nil];
    
    [UIView setAnimationDuration:3];//动画运行时间为1秒
    //[UIView setAnimationDelay:3];//延时.5秒
    [UIView setAnimationRepeatCount:1];//动画重复次数
    //[UIView setAnimationDelegate:self];//设置动画的代理
    //[UIView setAnimationWillStartSelector:@selector(startAnimation)];//动画将要开始时执行的方法
    //[UIView setAnimationDidStopSelector:@selector(stopAnimation)];//动画结束时执行的方法
    [UIView setAnimationBeginsFromCurrentState:YES];//设置是否从当前状态开始播放动画
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//设置动画时间曲线
    //    第一个参数：UIViewAnimationTransition的枚举值如下
    //    UIViewAnimationTransitionNone,              //不使用动画
    //    UIViewAnimationTransitionFlipFromLeft,      //从左向右旋转翻页
    //    UIViewAnimationTransitionFlipFromRight,     //从右向左旋转翻页
    //    UIViewAnimationTransitionCurlUp,            //从下往上卷曲翻页
    //    UIViewAnimationTransitionCurlDown,          //从上往下卷曲翻页
    //    第二个参数：需要过渡效果的View
    //    第三个参数：是否使用视图缓存，YES：视图在开始和结束时渲染一次；NO：视图在每一帧都渲染
    
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:_imageV cache:YES];
    
    _imageV.frame = CGRectMake(150, 250, 180 , 180);
    _imageV.image = [UIImage imageNamed:@"111"];
    
    [UIView commitAnimations];//动画提交
    
    
    
}
//1. 普通动画-->时间 动画
-(void)animation1{
    
    [UIView animateWithDuration:3.f animations:^{
        
        self.imageV.alpha = .3;
        self.imageV.frame = CGRectMake(160, 600, 80, 30);
        
    }];
    
}
//2. 普通动画-->时间 动画 动画之后block
-(void)animation2{
    
    [UIView animateWithDuration:3.f animations:^{
        self.imageV.alpha = .3;
        self.imageV.frame = CGRectMake(160, 400, 80, 30);
        
    } completion:^(BOOL finished) {
        //还可以嵌套动画
        [UIView animateWithDuration:1.f animations:^{
            
        }];
        
    }];
    
}
//3. 普通动画-->时间 延时 过度效果 动画之后block

-(void)animation3{
    [UIView animateWithDuration:3.f delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.imageV.alpha = .3;
        self.imageV.frame = CGRectMake(160, 600, 80, 30);
        
    } completion:^(BOOL finished) {
        
    }];
    {
        //过渡效果,可以组合使用
        //UIViewAnimationOptionLayoutSubviews            //进行动画时布局子控件
        //UIViewAnimationOptionAllowUserInteraction      //进行动画时允许用户交互
        //UIViewAnimationOptionBeginFromCurrentState     //从当前状态开始动画
        //UIViewAnimationOptionRepeat                    //无限重复执行动画
        //UIViewAnimationOptionAutoreverse               //执行动画回路
        //UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套动画的执行时间设置
        //UIViewAnimationOptionOverrideInheritedCurve    //忽略嵌套动画的曲线设置
        //UIViewAnimationOptionAllowAnimatedContent      //转场：进行动画时重绘视图
        //UIViewAnimationOptionShowHideTransitionViews   //转场：移除（添加和移除图层的）动画效果
        //UIViewAnimationOptionOverrideInheritedOptions  //不继承父动画设置
        //
        //UIViewAnimationOptionCurveEaseInOut            //时间曲线，慢进慢出（默认值）
        //UIViewAnimationOptionCurveEaseIn               //时间曲线，慢进
        //UIViewAnimationOptionCurveEaseOut              //时间曲线，慢出
        //UIViewAnimationOptionCurveLinear               //时间曲线，匀速
        //
        //UIViewAnimationOptionTransitionNone            //转场，不使用动画
        //UIViewAnimationOptionTransitionFlipFromLeft    //转场，从左向右旋转翻页
        //UIViewAnimationOptionTransitionFlipFromRight   //转场，从右向左旋转翻页
        //UIViewAnimationOptionTransitionCurlUp          //转场，下往上卷曲翻页
        //UIViewAnimationOptionTransitionCurlDown        //转场，从上往下卷曲翻页
        //UIViewAnimationOptionTransitionCrossDissolve   //转场，交叉消失和出现
        //UIViewAnimationOptionTransitionFlipFromTop     //转场，从上向下旋转翻页
        //UIViewAnimationOptionTransitionFlipFromBottom  //转场，从下向上旋转翻页
    }
}

//4. 弹簧动画-->时间 延时 震动效果(范围0~1，数值越小震动效果越明显) 初始速度(大=快) 过度效果  动画  动画后的block
-(void)animation4{
    
    [UIView animateWithDuration:3.f delay:0 usingSpringWithDamping:.5 initialSpringVelocity:20 options:UIViewAnimationOptionCurveEaseIn animations:^{
        
        self.imageV.frame = CGRectMake(150,300 , 100, 100);
        
    } completion:^(BOOL finished) {
        
        
    }];
    
}

//5. 关键帧动画-->时间 延时 过渡效果 动画 动画之后block
-(void)animation5
{
    [UIView animateKeyframesWithDuration:5.0 delay:0.f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
        
        [UIView addKeyframeWithRelativeStartTime:0.f relativeDuration:1.0 / 4 animations:^{
            self.centerShow.backgroundColor = [UIColor colorWithRed:0.9475 green:0.1921 blue:0.1746 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:1.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.centerShow.backgroundColor = [UIColor colorWithRed:0.1064 green:0.6052 blue:0.0334 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:2.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.centerShow.backgroundColor = [UIColor colorWithRed:0.1366 green:0.3017 blue:0.8411 alpha:1.0];
        }];
        [UIView addKeyframeWithRelativeStartTime:3.0 / 4 relativeDuration:1.0 / 4 animations:^{
            self.centerShow.backgroundColor = [UIColor colorWithRed:0.619 green:0.037 blue:0.6719 alpha:1.0];
        }];
    } completion:^(BOOL finished) {
        NSLog(@"动画结束");
    }];
    
    {
        //过渡效果,可以组合使用
        //UIViewAnimationOptionLayoutSubviews           //进行动画时布局子控件
        //UIViewAnimationOptionAllowUserInteraction     //进行动画时允许用户交互
        //UIViewAnimationOptionBeginFromCurrentState    //从当前状态开始动画
        //UIViewAnimationOptionRepeat                   //无限重复执行动画
        //UIViewAnimationOptionAutoreverse              //执行动画回路
        //UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套动画的执行时间设置
        //UIViewAnimationOptionOverrideInheritedOptions //不继承父动画设置
        //
        //UIViewKeyframeAnimationOptionCalculationModeLinear     //运算模式 :连续
        //UIViewKeyframeAnimationOptionCalculationModeDiscrete   //运算模式 :离散
        //UIViewKeyframeAnimationOptionCalculationModePaced      //运算模式 :均匀执行
        //UIViewKeyframeAnimationOptionCalculationModeCubic      //运算模式 :平滑
        //UIViewKeyframeAnimationOptionCalculationModeCubicPaced //运算模式 :平滑均匀
        
    }
    
}
//6. 转场动画-->将要转出的view 时间 过渡效果 动画 动画后的block
-(void)animation6{
    
    [UIView transitionWithView:self.imageV duration:2.f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        
        self.imageV.image = [UIImage imageNamed:@"111"];
    } completion:^(BOOL finished) {
    }];
    
}


-(UIImageView *)imageV{
    
    if (!_imageV) {
        _imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"000"]];
        _imageV.frame = CGRectMake(20, 70, 100, 100);
        _imageV.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imageV;
}
@end
